package com.tool_stdy.RedisTest;

import java.util.List;
import java.util.Set;

import com.google.common.collect.Lists;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Transaction;

public class RedisTest {

    /**
     * 获取多个key
     */
    public static void testKeys() {
        Jedis jedis = RedisCacheClient.getInstrance().getClient();
        Set<String> keys = jedis.keys("cvfeedBackHandl_*");
        for (String key : keys) {
            System.out.println(key);
        }
    }

    /**
     * 多次get
     */
    public static void testMget() {
        Jedis jedis = RedisCacheClient.getInstrance().getClient();
        Set<String> keys = jedis.keys("cvfeedBackHandl_*");
        List<String> result = Lists.newArrayList();
        long t1 = System.currentTimeMillis();
        for (String key : keys) {
            result.add(jedis.get(key));
        }
        for (String src : result) {
            System.out.println(src);
        }
        System.out.println(System.currentTimeMillis() - t1);
    }

    /**
     * 管道
     */
    public static void testPipline() {
        Jedis jedis = RedisCacheClient.getInstrance().getClient();
        Set<String> keys = jedis.keys("cvfeedBackHandl_*");
        List<Object> result = Lists.newArrayList();
        Pipeline pipelined = jedis.pipelined();
        long t1 = System.currentTimeMillis();
        for (String key : keys) {
            pipelined.get(key);
            pipelined.del("testabcd");
        }
        result = pipelined.syncAndReturnAll();
        for (Object src : result) {
            System.out.println(src);
        }
        System.out.println("===========");
        System.out.println(System.currentTimeMillis() - t1);
    }

    /**
     * 事务
     */
    public static void testMulit() {
        Jedis jedis = RedisCacheClient.getInstrance().getClient();
        Set<String> keys = jedis.keys("cvfeedBackHandl_*");
        Transaction multi = jedis.multi();
        List<Object> result = Lists.newArrayList();
        long t1 = System.currentTimeMillis();
        for (String key : keys) {
            multi.get(key);
            multi.set("1", "2");
            multi.del("1");
        }
        result = multi.exec();
        for (Object src : result) {
            System.out.println(src);
        }
        System.out.println(System.currentTimeMillis() - t1);
    }

    /**
     * watch + 事务
     */
    public static void testWach() {
        Jedis jedis = RedisCacheClient.getInstrance().getClient();
        String watch = jedis.watch("testabcd");
        System.out.println(Thread.currentThread().getName() + "--" + watch);
        Pipeline pipelined = jedis.pipelined();
        pipelined.set("testabcd", "23432");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<Object> exec = pipelined.syncAndReturnAll();
        System.out.println("---" + exec);
        jedis.unwatch();
    }

    /**
     * watch + 事务
     */
    public static void testWatch2() {
        Jedis jedis = RedisCacheClient.getInstrance().getClient();
        String watch = jedis.watch("testabcd");
        System.out.println(Thread.currentThread().getName() + "--" + watch);
        Pipeline pipelined = jedis.pipelined();
        pipelined.set("testabcd", "125");
        List<Object> exec = pipelined.syncAndReturnAll();
        System.out.println("--->>" + exec);
    }

    public static void testWatchMulity() {
        new Thread(new Runnable() {
            public void run() {
                testWach();
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                testWatch2();
            }
        }).start();
    }

    public static void main(String[] args) throws InterruptedException {
        // testKeys();
        //testMget();
        //  testPipline();
        //testMulit();
        Thread.sleep(5000);
        System.out.println("over");
    }
}
